package com.styytech.dadabao.ui.pullrefreshfragment;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.utils.NetworkUtils;
import com.styytech.dadabao.widge.pulltorefresh.PullToRefreshListView;

public class ComonListFragment extends ComonGetDataFragment implements
        InterfaceBaseFragment {
    private View rootView;
    protected ListView mListView;


//    /**加载框*/
//    protected LinearLayout ll_loginInformation;
//    /**加载中图标*/
//    protected ImageView pb_loadProgressBar;
//    /**提示语*/
//    protected TextView tv_infor;
//    /**重试*/
//    protected ImageView iv_reloadBtn;

    protected PullToRefreshListView mPullRefreshListView;

    private String dataUrl;
    private int adapterType = -1;

//    private boolean isFirst = true;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        super.onCreateView(inflater, container, savedInstanceState);

        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {

            dataUrl = getArguments().getString("dataUrl");
            adapterType = getArguments().getInt("adapterType");

            String Spair = null;
            // super.params = new ArrayList<NameValuePair>();
            if (getArguments().getString("value") != null) {
                Spair = getArguments().getString("value");
            }

            // else if (adapterType == Constants.ADAPTER_TYPE_JOINUS) { // 加入大大
            // if (Spair != null) {
            // super.params.add("jobSite", Spair);
            // }
            // }
            rootView = super.onCreateView(inflater, container,
                    savedInstanceState, R.layout.fragment_common_getlist);

            initView(rootView); // 初始化控件
            setInterface(this);
        }

        return rootView;

    }

    public void initView(View rootView) {

//        ll_loginInformation = (LinearLayout) rootView
//                .findViewById(R.id.ll_loginInformation);
//        pb_loadProgressBar = (ImageView) rootView
//                .findViewById(R.id.pb_loadProgressBar);
//        tv_infor = (TextView) rootView.findViewById(R.id.tv_infor);
//        iv_reloadBtn = (ImageView) rootView
//                .findViewById(R.id.iv_reloadBtn);
//        iv_reloadBtn.setOnClickListener(this);
        mPullRefreshListView = (PullToRefreshListView) rootView
                .findViewById(R.id.lv_dataList);

        mListView = mPullRefreshListView.getRefreshableView();
        mListView.setDivider(null);
        mListView.setOnItemClickListener(this); // 列表点击监听
        mPullRefreshListView.setOnRefreshListener(mOnrefreshListener);// 下拉监听

        if (pb_loadProgressBar != null) {
            AnimationDrawable anim = (AnimationDrawable) pb_loadProgressBar
                    .getBackground();
            if (anim != null) {
                anim.start();
            }
        }

        super.setView(mPullRefreshListView);
    }

    @Override
    public void onResume() {
//        if (isFirst) {
//            refreshData();
//        }
        super.onResume();
    }

    public ListView getmListView() {
        return mListView;
    }

    public void setmListView(ListView mListView) {
        this.mListView = mListView;
    }

    @Override
    protected void connectTivityService() {
        refreshData();
        super.connectTivityService();
    }

    @Override
    protected void connectTivityErroe() {
        setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "网络连接异常，请检查网络连接");
        super.connectTivityErroe();
    }

    /**
     * 刷新数据
     */
    @Override
    public void refreshData() {

        if (!NetworkUtils.isNetworkAvailable(getmActivity())) {// 网络链接异常
            setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "网络连接异常，请检查网络连接");
            return;
        }
//        if (isFirst) {
        if (dataUrl == null) {
            setView(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE, "服务器地址不能为空");
            return;
        }
//            isFirst = !isFirst;
        setView(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE, "正在获取数据，请稍等...");
//        }

        getListData(PAGE_NUM_FIRST);
        super.refreshData();
    }

    /**
     * 没有数据加载时界面显示
     */
    @Override
    public void setEmptyDateShow(int httpType) {
        switch (httpType) {
//		case ConstantsCommon.DATA_SRC_NEWS:
//			setView(View.VISIBLE, View.GONE, View.VISIBLE, "暂时没有新闻");
//			break;
            default:
                setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "暂时没有消息");
                break;
        }
    }

    @Override
    public void setFailureView(String msg) {
        setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "" + msg);
    }

    /**
     * 数据加载完毕
     */
    @Override
    public void onRefreshComplete() {
        setView(View.GONE, View.VISIBLE, View.GONE, View.VISIBLE, "");
        mPullRefreshListView.onRefreshComplete();
    }

    /**
     * 加载列表数据
     */
    @Override
    public void setAdapter(BaseAdapter adapter) {
        mListView.setAdapter(adapter);
    }

    /**
     * 设置提示信息
     *
     * @param visiLinearLayout 总体布局是否可见
     * @param visiProgressBar  加载框是否可见
     * @param visiTextView     提示信息组件
     * @param info             提示信息
     */
    public void setView(int visiLinearLayout, int visiProgressBar,
                        int visiTextView, int visiImageButn, String info) {
        if (ll_loginInformation != null)
            ll_loginInformation.setVisibility(visiLinearLayout);
        if (pb_loadProgressBar != null)
            pb_loadProgressBar.setVisibility(visiProgressBar);
        if (tv_infor != null)
            tv_infor.setVisibility(visiTextView);
        if (iv_reloadBtn != null)
            iv_reloadBtn.setVisibility(visiImageButn);
        if (tv_infor != null && info != null) {
            tv_infor.setText(info);
        }
    }

    /**
     * 刷新按钮
     */
    protected void reloadBtnOnclick() {
        refreshData();
    }
//    @Override
//    public void onClick(View v) {
//
//    }
}
